<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #box {
        width: 200px;
        height: 200px;
        background-color: skyblue;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <button id="btn">显示/隐藏</button>
    <script>
      let isShow = true; // 记录盒子是否显示，默认true代表显示
      // 1. 找到id=btn的元素
      let btnEle = document.getElementById("btn");
      let boxEle = document.getElementById("box");
      // 2. 给其绑定单击事件
      btnEle.addEventListener("click", function () {
        if (isShow) {
          boxEle.style.display = "none"; // 隐藏
          //   isShow = false;
        } else {
          boxEle.style.display = ""; // 显示
          //   isShow = true;
        }

        isShow = !isShow; // 取反操作
      });
    </script>
  </body>
</html>
